class IncentiveService
  attr_reader :incentive, :incentive_image 

  def initialize(incentive, incentive_image) 
    @incentive = incentive 
    @incentive_image = incentive_image 
  end 

  def save 
    return false unless valid?
    begin 
      Incentive.transaction do 
        if @incentive_image.new_record? 
          @incentive.incentive_image.destroy if @incentive.incentive_image 
          @incentive_image.incentive = @incentive
          @incentive_image.save! 
        end
        @incentive.reload # needed to avoid "frozen hash" error -mdorn
        @incentive.save! 
        true 
      end 
    rescue 
      false 
    end 
  end 

  def valid? 
    @incentive.valid? && @incentive_image.valid? 
  end

  def update_attributes(incentive_attributes, incentive_image_file) 
    @incentive.attributes = incentive_attributes
    unless incentive_image_file.blank?
      @incentive_image = IncentiveImage.new(:uploaded_data => incentive_image_file) 
    end 
    save
  end 

end 
